﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WorldOnFire
{
    public class CWarpGate : ABCObject
    {
        private CWaveManager.eReleaseRate[] m_eReleaseRate;
        private float[] m_fGateTimer;
        private bool[] m_bWarpCharging;

        public bool IsWarpReady(int _nIndex)
        { return m_fGateTimer[_nIndex] < 0.0f; }

        public void ResetWarpTimer(int _nIndex)
        {
            switch ((int)m_eReleaseRate[_nIndex])
            {
                case (int)CWaveManager.eReleaseRate.RON:
                    {
                        m_fGateTimer[_nIndex] = 25.0f;
                    }
                    break;
                case (int)CWaveManager.eReleaseRate.SLOW:
                    {
                        m_fGateTimer[_nIndex] = 15.0f;
                    }
                    break;
                case (int)CWaveManager.eReleaseRate.MEDIUM:
                    {
                        m_fGateTimer[_nIndex] = 10.0f;
                    }
                    break;
                case (int)CWaveManager.eReleaseRate.FAST:
                    {
                        m_fGateTimer[_nIndex] = 5.0f;
                    }
                    break;
                case (int)CWaveManager.eReleaseRate.RED_BALLS:
                    {
                        m_fGateTimer[_nIndex] = 2.5f;
                    }
                    break;
            }
        }

        private CRenderingManager.eTextureID m_eIlluminationTextureID;

        public override void Initialize(int _nObjectCount)
        {
            m_eReleaseRate = new CWaveManager.eReleaseRate[_nObjectCount];
            m_fGateTimer = new float[_nObjectCount];
            m_bWarpCharging = new bool[_nObjectCount];

            base.Initialize(_nObjectCount);

            m_bOverlay = true;
        }

        public override void LoadContent()
        {
            m_eModelID = CRenderingManager.eModelID.ENEMY_WARP_GATE;
            m_eTextureID = CRenderingManager.eTextureID.ENEMY;
            m_eIlluminationTextureID = CRenderingManager.eTextureID.ENEMY_ILLUMINATION;

            base.LoadContent();
        }

        public int CreateWarpGate(float _fInitialDelay, CWaveManager.eReleaseRate _eReleaseRate)
        {
            // If there is an unused projectile
            if (this.FindUnusedIndex() == true)
            {
                // Create the warp gate
                this.CreateObject(m_nInactiveObjectIndex);

                // Initialize the warp gate
                m_eReleaseRate[m_nInactiveObjectIndex] = _eReleaseRate;
                m_fGateTimer[m_nInactiveObjectIndex] = _fInitialDelay;
                m_mTransform[m_nInactiveObjectIndex] = Matrix.CreateTranslation(0.0f, 0.0f, -1000.0f);
                m_mTransform[m_nInactiveObjectIndex] = m_mTransform[m_nInactiveObjectIndex] 
                    * Matrix.CreateRotationY(s_Ron.NextFloat(-0.0f, 2.0f * (float)Math.PI));

                return m_nInactiveObjectIndex;
            }

            return -1;
        }

        public override void Update(float _fDeltaTime)
        {
            for (int iObject = 0; iObject < m_nObjectCount; ++iObject)
            {
                m_fGateTimer[iObject] -= _fDeltaTime;
            }

            base.Update(_fDeltaTime);
        }

        public override void Render(GraphicsDevice _GraphicsDevice)
        {
            Effect EnemyEffect = s_RenderingManager.GetEffect(m_eShaderID);
            EnemyEffect.Parameters["g_IlluminationMap"].SetValue(s_RenderingManager.GetTexture(m_eIlluminationTextureID));

            base.Render(_GraphicsDevice);
        }
    }
}
